/**   
 * @Title: ShuangseqiuHemaiDetailAct.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact.shuangseqiuagp 
 * @Description: (合买详情&参与合买) 
 * @author: City Jiangsy@500wan.com   
 * @date: 2011-7-4 下午12:09:16  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact.shuangseqiuagp;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.HemaiDetailInfo;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.GetUserIconCommunicater;
import com.esun.rabbit2.communicaters.HemaiGetDetailCommunicater;
import com.esun.rabbit2.communicaters.HemaiJoinCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.OptionAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.HelpAct;
import com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord.LotteryRecordDetailMoreAct;
import com.esun.rabbit2.viewer.mainact.mylotteryact.recharg.RechargeMenuPage;
import com.esun.ui.R;
import com.esun.util.LoginTimeControl;
import com.esun.util.LotteryUtils;
import com.esun.util.ShowZhanji;
import com.esun.util.ViewerUtil;
import com.esun.util.img.ImageUtils;
import com.esun.util.img.db.ImageDB;
import com.esun.util.log.EDebug;

/**
 * @ClassName: ShuangseqiuHemaiDetailAct
 * @Description: 合买详情&参与合买
 * @author: City Jiangsy@500wan.com
 * @date: 2011-7-4 下午12:09:16
 * 
 */
public class ShuangseqiuHemaiDetailAct extends Activity implements OnClickListener {

	private TextView tvTerm;
	private TextView tvTime;
	private TextView tvProId;
	private TextView tvRestCount;
	private TextView tvJoinMoney;
	private TextView tvBalance;

	private TextView tvFaqiren;
	private TextView tvHitCount;
	private TextView tvHitMoney;
	private TextView tvAllMoney;
	private TextView tvMultiple;
	private TextView tvAllCount;
	private TextView tvEachMoney;
	private TextView tvBaodiMoney;
	private TextView tvTicheng;
	private TextView tvState;
	private TextView tvJindu;
	private TextView tvRestCountInside;
	private TextView tvIsShow;
	private TextView tvRengouCount;
	private ImageView imageFaqiren;

	private EditText edtJoinCount;
	private ProgressDialog progress;
	private LinearLayout linearZhanji;
	private LinearLayout linearContent;
	private LinearLayout linearLayOutZhanji;

	private ImageButton btnCancel;
	private ImageButton btnHelp;
	private Button btnSubmit;
	private Button btnConect;
	private ProgressDialog progressJoin;

	private LoginTimeControl loginTimeControl;

	private static final String NO_DATA = "--";

	private HemaiGetDetailCommunicater hemaiGetDetailCommunicater;
	private GetUserIconCommunicater getUserIconCommunicater;
	private HemaiJoinCommunicater hemaiJoinCommunicater;

	private Handler handlerHemaiDetail;

	private static final int GET_DETAIL_SUCCEED = 1001;
	private static final int GET_DETAIL_FAIL = 1002;

	private static final int JOIN_SUCCEED = 1003;
	private static final int JOIN_FAIL = 1004;

	private static final int REPLACE_IMAGE = 1006;
	private static final int GET_ICONNAME_SUCCEED = 1007;
	private static final int GET_ICONNAME_FAIL = 1008;
	private int multiple;
	//战绩金钻
	private String golddiamond;
	//战绩银钻
	private String silverdiamond;
	//战绩金星
	private String goldstar;
	//战绩银星
	private String silverstar;
	//战绩金杯
	private String goldcup;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shuangseqiu_hemai_detail_act);
		initHandler();
		initUi();
		
	}

	private void initHandler() {
		handlerHemaiDetail = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case REPLACE_IMAGE:
					Bitmap bm = (Bitmap) msg.obj;
					imageFaqiren.setImageBitmap(bm);

					break;
				case GET_DETAIL_SUCCEED:
					hemaiGetDetailCommunicater.updateHemaiDetailInfo();
					btnSubmit.setEnabled(true);
					progress.dismiss();
					updateTvContent();
					break;

				case GET_DETAIL_FAIL:
					btnSubmit.setEnabled(true);
					progress.dismiss();
					//ViewerUtil.showTipDialog(ShuangseqiuHemaiDetailAct.this, hemaiGetDetailCommunicater.getErrorMSG(), false);
					Toast.makeText(ShuangseqiuHemaiDetailAct.this, hemaiGetDetailCommunicater.getErrorMSG(), 0).show();
					break;

				case JOIN_SUCCEED:
					buySucceed();
					progressJoin.dismiss();
					break;

				case JOIN_FAIL:
					if (hemaiJoinCommunicater.getState() == 0) {
						ViewerUtil.showTipDialog(ShuangseqiuHemaiDetailAct.this, hemaiJoinCommunicater.getErrorMSG() + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, false);
					} else if (hemaiJoinCommunicater.getState() == 105) {
						popBalanceNotEnoughAlert();
					} else {
						//ViewerUtil.showTipDialog(ShuangseqiuHemaiDetailAct.this, hemaiJoinCommunicater.getErrorMSG(), false);
						Toast.makeText(ShuangseqiuHemaiDetailAct.this, hemaiJoinCommunicater.getErrorMSG(), 0).show();
					}
					progressJoin.dismiss();
					break;

				case Config.CONNECTION_FAIL:
					try {
						progressJoin.dismiss();
					} catch (Exception e) {
						// TODO: handle exception
					}
					btnSubmit.setEnabled(true);
					progress.dismiss();
					//ViewerUtil.showTipDialog(ShuangseqiuHemaiDetailAct.this, Language.STR_CONNECTIONFAIL, false);
					Toast.makeText(ShuangseqiuHemaiDetailAct.this, Language.STR_CONNECTIONFAIL, 0).show();
					break;

				case GET_ICONNAME_SUCCEED:
					getAvatar();
					break;

				case GET_ICONNAME_FAIL:
					EDebug.o(ShuangseqiuHemaiDetailAct.this, "get icon name fail");
					break;

				default:
					break;
				}
			}

		};

	}

	private Dialog dialogBalanceNotEnough;
	private View.OnClickListener moneyNotEnoughListener;

	private void popBalanceNotEnoughAlert() {
		if (dialogBalanceNotEnough == null) {
			dialogBalanceNotEnough = new Dialog(this, R.style.dialog);
		}

		if (moneyNotEnoughListener == null) {
			moneyNotEnoughListener = new OnClickListener() {

				@Override
				public void onClick(View v) {
					switch (v.getId()) {
					case R.id.shiyixuanwu_alert_money_not_enough_btn_cancel:
						break;

					case R.id.shiyixuanwu_alert_money_not_enough_btn_recharge:

						ShuangseqiuHemaiDetailAct.this.startActivity(new Intent().setClass(ShuangseqiuHemaiDetailAct.this, RechargeMenuPage.class));

						break;
					}

					dialogBalanceNotEnough.dismiss();
				}
			};
		}

		View view = this.getLayoutInflater().inflate(R.layout.shiyixuanwu_alert_money_not_enough, null);

		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_cancel)).setOnClickListener(moneyNotEnoughListener);
		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_recharge)).setOnClickListener(moneyNotEnoughListener);

		dialogBalanceNotEnough.setContentView(view);
		dialogBalanceNotEnough.show();
	}

	private void updateTvContent() {
		eachMoney = HemaiDetailInfo.getHemaiDetailInfo().getEachMoney();
		tvJoinMoney.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(Double.parseDouble(eachMoney) * multiple));

		/** 2011-08-08 city add for balance set */
		tvBalance.setText(getLotRestBottom());

		tvHitCount.setText(HemaiDetailInfo.getHemaiDetailInfo().getHitCount());
		tvHitCount.setTextColor(getResources().getColor(R.color.kjxx_blue));
		tvHitMoney.setText(Language.POPLOTTERYTIP_RMB + HemaiDetailInfo.getHemaiDetailInfo().getHitMoney());
		tvHitMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		tvAllMoney.setText(Language.POPLOTTERYTIP_RMB + HemaiDetailInfo.getHemaiDetailInfo().getAllMoney());
		tvAllMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		tvMultiple.setText(HemaiDetailInfo.getHemaiDetailInfo().getBeishu());
		tvAllCount.setText(HemaiDetailInfo.getHemaiDetailInfo().getAllCount());
		tvEachMoney.setText(Language.POPLOTTERYTIP_RMB + HemaiDetailInfo.getHemaiDetailInfo().getEachMoney());
		tvEachMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		tvBaodiMoney.setText(Language.POPLOTTERYTIP_RMB + HemaiDetailInfo.getHemaiDetailInfo().getBaodiMoney());
		tvBaodiMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		tvTicheng.setText(HemaiDetailInfo.getHemaiDetailInfo().getTicheng() + Config.PERCENT);
		tvState.setText(HemaiDetailInfo.getHemaiDetailInfo().getState());
		tvJindu.setText(HemaiDetailInfo.getHemaiDetailInfo().getJindu() + Config.PERCENT);
		tvRestCountInside.setText(HemaiDetailInfo.getHemaiDetailInfo().getLeftCount());
		tvTime.setText(HemaiDetailInfo.getHemaiDetailInfo().getAddTime());
		tvRengouCount.setText(Integer.toString(multiple));
		tvRestCount.setText(Integer.toString(Integer.parseInt(HemaiDetailInfo.getHemaiDetailInfo().getLeftCount()) - multiple));

		edtJoinCount.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {
					multiple = Integer.parseInt(String.valueOf(s));
					EDebug.o(ShuangseqiuHemaiDetailAct.this, "multiple is " + multiple);
					int max = Integer.parseInt(HemaiDetailInfo.getHemaiDetailInfo().getLeftCount());
					Toast multipleToast = Toast.makeText(ShuangseqiuHemaiDetailAct.this, Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + Integer.toString(max)
							+ Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
					multipleToast.setGravity(Gravity.CENTER, 0, 0);
					tvJoinMoney.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(Double.parseDouble(eachMoney) * multiple));
					tvJoinMoney.setTextColor(getResources().getColor(R.color.solid_black));

					/** 2011-08-08 city add for balance set */
					tvBalance.setText(getLotRestBottom());

					canJoin = true;
					tvRengouCount.setText(Integer.toString(multiple));
					tvRestCount.setText(Integer.toString(Integer.parseInt(HemaiDetailInfo.getHemaiDetailInfo().getLeftCount()) - multiple));

					if (multiple > max) {
						multipleToast.show();
						multiple = max;
						edtJoinCount.setText(Integer.toString(multiple));
						tvJoinMoney.setText(Language.POPLOTTERYTIP_RMB + getDoubleString(Double.parseDouble(eachMoney) * multiple));

						/** 2011-08-08 city add for balance set */
						tvBalance.setText(getLotRestBottom());

						tvRengouCount.setText(Integer.toString(multiple));
						tvRestCount.setText(Integer.toString(Integer.parseInt(HemaiDetailInfo.getHemaiDetailInfo().getLeftCount()) - multiple));
						EDebug.o(ShuangseqiuHemaiDetailAct.this, "multiple is " + multiple);
					} else if (multiple < 1) {
						canJoin = false;
						multipleToast.show();
						tvJoinMoney.setText(Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + Integer.toString(max) + Language.STR_ZHIJIANDESHUZI);
						tvJoinMoney.setTextColor(getResources().getColor(R.color.dark_red));

						/** 2011-08-08 city add for balance set */
						tvBalance.setText(getLotRestBottom());
						EDebug.o(ShuangseqiuHemaiDetailAct.this, "multiple is " + multiple);

					}
				} else {
					canJoin = false;
					multiple = 0;
					tvJoinMoney.setText(Language.POPLOTTERYTIP_RMB + Double.toString(Double.parseDouble(eachMoney) * multiple));

					/** 2011-08-08 city add for balance set */
					tvBalance.setText(getLotRestBottom());

					tvRengouCount.setText(Integer.toString(multiple));
					tvRestCount.setText(Integer.toString(Integer.parseInt(HemaiDetailInfo.getHemaiDetailInfo().getLeftCount()) - multiple));
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		setProjectContent();

	}

	private void setProjectContent() {
		// TODO Auto-generated method stub

		if (HemaiDetailInfo.getHemaiDetailInfo().getProgramContent().contains("txt")) {
			if (HemaiDetailInfo.getHemaiDetailInfo().getIsUpload().equals(Config.NUM1)) {
				if ((HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals(Config.NUM0))
						|| (HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals("2") && HemaiDetailInfo.getHemaiDetailInfo().getIsChaser().equals(Config.NUM1))) {
					String strContent = HemaiDetailInfo.getHemaiDetailInfo().getProgramContent();
					if (strContent.indexOf("txt") > 0) {
						Button btnMore = new Button(this);
						btnMore.setText("详细内容");
						btnMore.setBackgroundResource(R.drawable.lotteryrecord_detail_btn_more);
						btnMore.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								String strUrl = "http://m.500wan.com/rabbit2/webview/find_txt.php?lotid=" + lotId + "&playid=" + playId + "&pid=" + programId;
								EDebug.o(ShuangseqiuHemaiDetailAct.this, strUrl);
								Intent intentToHelp = new Intent();
								intentToHelp.setClass(ShuangseqiuHemaiDetailAct.this, HelpAct.class);
								intentToHelp.putExtra("url", strUrl).putExtra("title", "方案内容").putExtra("havetitle", Config.NUM1);
								startActivity(intentToHelp);

							}
						});

						linearContent.addView(btnMore);
					} else {
						ArrayList<HashMap<String, Object>> listResult = new ArrayList<HashMap<String, Object>>();
						listResult = getListContent(strContent);
						if (listResult.size() < 8) {
							for (int i = 0; i < listResult.size(); i++) {
								LinearLayout linear = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.choosenormalbuy_linear_item, null);
								TextView tvBlue = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_blue);
								TextView tvRed = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_red);
								tvRed.setText(strLengthOption((String) listResult.get(i).get("red"), 20));
								tvBlue.setText(strLengthOption((String) listResult.get(i).get("blue"), 11));
								linearContent.addView(linear);
							}
						} else {
							for (int i = 0; i < 7; i++) {
								LinearLayout linear = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.choosenormalbuy_linear_item, null);
								TextView tvBlue = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_blue);
								TextView tvRed = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_red);
								tvRed.setText(strLengthOption((String) listResult.get(i).get("red"), 20));
								tvBlue.setText(strLengthOption((String) listResult.get(i).get("blue"), 11));
								linearContent.addView(linear);
							}
							Button btnMore = new Button(this);
							btnMore.setText("...");
							btnMore.setBackgroundResource(R.drawable.lotteryrecord_detail_btn_more);
							btnMore.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									String[] strContent = HemaiDetailInfo.getHemaiDetailInfo().getProgramContent().replace(',', ' ').split("\\$");
									Intent intentToDetail = new Intent();
									intentToDetail.setClass(ShuangseqiuHemaiDetailAct.this, LotteryRecordDetailMoreAct.class);
									intentToDetail.putExtra("classify", "txt").putExtra("content", strContent).putExtra(Key.KEY_LOTID, lotId);
									startActivity(intentToDetail);
								}
							});
							linearContent.addView(btnMore);
						}
					}

					// }
					// else if
					// (HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals(Config.NUM1))
					// {
					// tvIsShow.setVisibility(View.VISIBLE);
					// tvIsShow.setText(Language.POPLOTTERYTIP_GENDANKEKAN);
				} else if (HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals("2") && HemaiDetailInfo.getHemaiDetailInfo().getIsChaser().equals(Config.NUM0)) {
					tvIsShow.setVisibility(View.VISIBLE);
					tvIsShow.setText("仅跟单人可见");

				} else {
					tvIsShow.setVisibility(View.VISIBLE);
					tvIsShow.setText(Language.POPLOTTERYTIP_JIEZHIHOUGONGKAI);
				}
			} else {
				tvIsShow.setVisibility(View.VISIBLE);
				tvIsShow.setText("方案未上传");
			}
		} else {
			if ((HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals(Config.NUM0))
					|| (HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals("2") && HemaiDetailInfo.getHemaiDetailInfo().getIsChaser().equals(Config.NUM1))) {
				String strContent = HemaiDetailInfo.getHemaiDetailInfo().getProgramContent();
				if (strContent.indexOf("txt") > 0) {
					Button btnMore = new Button(this);
					btnMore.setText("详细内容");
					btnMore.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							String strUrl = "http://m.500wan.com/rabbit2/webview/find_txt.php?lotid=" + lotId + "&playid=" + playId + "&pid=" + programId;
							EDebug.o(ShuangseqiuHemaiDetailAct.this, strUrl);
							Intent intentToHelp = new Intent();
							intentToHelp.setClass(ShuangseqiuHemaiDetailAct.this, HelpAct.class);
							intentToHelp.putExtra("url", strUrl).putExtra("title", "方案内容").putExtra("havetitle", Config.NUM1);
							startActivity(intentToHelp);

						}
					});

					linearContent.addView(btnMore);
				} else {
					ArrayList<HashMap<String, Object>> listResult = new ArrayList<HashMap<String, Object>>();
					listResult = getListContent(strContent);
					if (listResult.size() < 8) {
						for (int i = 0; i < listResult.size(); i++) {
							LinearLayout linear = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.choosenormalbuy_linear_item, null);
							TextView tvBlue = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_blue);
							TextView tvRed = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_red);
							tvRed.setText(strLengthOption((String) listResult.get(i).get("red"), 20));
							tvBlue.setText(strLengthOption((String) listResult.get(i).get("blue"), 11));
							linearContent.addView(linear);
						}
					} else {
						for (int i = 0; i < 7; i++) {
							LinearLayout linear = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.choosenormalbuy_linear_item, null);
							TextView tvBlue = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_blue);
							TextView tvRed = (TextView) linear.findViewById(R.id.choosenormalbuylinearitem_tv_red);
							tvRed.setText(strLengthOption((String) listResult.get(i).get("red"), 20));
							tvBlue.setText(strLengthOption((String) listResult.get(i).get("blue"), 11));
							linearContent.addView(linear);
						}
						Button btnMore = new Button(this);
						btnMore.setText("...");
						btnMore.setBackgroundResource(R.drawable.lotteryrecord_detail_btn_more);
						btnMore.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								String[] strContent = HemaiDetailInfo.getHemaiDetailInfo().getProgramContent().replace(',', ' ').split("\\$");
								Intent intentToDetail = new Intent();
								intentToDetail.setClass(ShuangseqiuHemaiDetailAct.this, LotteryRecordDetailMoreAct.class);
								intentToDetail.putExtra("classify", "txt").putExtra("content", strContent).putExtra(Key.KEY_LOTID, lotId);
								startActivity(intentToDetail);
							}
						});
						linearContent.addView(btnMore);
					}
				}

				// }
				// else if
				// (HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals(Config.NUM1))
				// {
				// tvIsShow.setVisibility(View.VISIBLE);
				// tvIsShow.setText(Language.POPLOTTERYTIP_GENDANKEKAN);
			} else if (HemaiDetailInfo.getHemaiDetailInfo().getIsShow().equals("2") && HemaiDetailInfo.getHemaiDetailInfo().getIsChaser().equals(Config.NUM0)) {
				tvIsShow.setVisibility(View.VISIBLE);
				tvIsShow.setText("仅跟单人可见");

			} else {
				tvIsShow.setVisibility(View.VISIBLE);
				tvIsShow.setText(Language.POPLOTTERYTIP_JIEZHIHOUGONGKAI);
			}
		}

	}

	private void threadGetUserIcon() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					boolean isSucceed = getUserIconCommunicater.isGetIconNameSucced(strfaqiren);
					if (isSucceed) {
						Message msg = handlerHemaiDetail.obtainMessage(GET_ICONNAME_SUCCEED, null);
						handlerHemaiDetail.sendMessage(msg);
					} else {
						Message msg = handlerHemaiDetail.obtainMessage(GET_ICONNAME_FAIL, null);
						handlerHemaiDetail.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerHemaiDetail.obtainMessage(GET_ICONNAME_FAIL, null);
					handlerHemaiDetail.sendMessage(msg);
				}
			}
		}.start();
	}

	private void threadGetDetail() {
		btnSubmit.setEnabled(false);
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					boolean isGetSucceed = hemaiGetDetailCommunicater.isGetDetailSucced(lotId, programId, strfaqiren);
					if (isGetSucceed) {
						Message msg = handlerHemaiDetail.obtainMessage(GET_DETAIL_SUCCEED, null);
						handlerHemaiDetail.sendMessage(msg);
					} else {
						Message msg = handlerHemaiDetail.obtainMessage(GET_DETAIL_FAIL, null);
						handlerHemaiDetail.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerHemaiDetail.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerHemaiDetail.sendMessage(msg);
				}
			}
		}.start();
	}

	private void threadJoin() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					// boolean isGetSucceed =
					// hemaiJoinCommunicater.isJoinSucceed(lotId, programId,
					// termNum, buyNum, playId, eachMoney);
					// boolean isGetSucceed =
					// hemaiJoinCommunicater.isJoinSucceed(lotId, programId,
					// termNum, buyNum,
					// Config.PLAYID_BEIJINGZUQIUDANCHANG_SHENGPINGFU);
					boolean isGetSucceed = hemaiJoinCommunicater.isJoinSucceed(lotId, programId, termNum, buyNum, playId, eachMoney);
					if (isGetSucceed) {
						Message msg = handlerHemaiDetail.obtainMessage(JOIN_SUCCEED, null);
						handlerHemaiDetail.sendMessage(msg);
					} else {
						Message msg = handlerHemaiDetail.obtainMessage(JOIN_FAIL, null);
						handlerHemaiDetail.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerHemaiDetail.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerHemaiDetail.sendMessage(msg);
				}
			}
		}.start();
	}

	private String lotId;
	private String programId;
	private String strfaqiren;
	private String showId;
	private String termNum;
	private String playId;
	private String eachMoney;
	private String buyNum;
	private boolean canJoin = true;

	private void initUi() {

		Bundle bundle = this.getIntent().getExtras();
		lotId = bundle.getString(Key.KEY_LOT_ID);
		programId = bundle.getString(Key.KEY_ID);
		strfaqiren = bundle.getString(Key.KEY_USER_NAME);
		showId = bundle.getString(Key.KEY_PROID);
		termNum = bundle.getString(Key.KEY_EXPECT);
		playId = bundle.getString(Key.KEY_PLAYTYPE);
		//金钻
		golddiamond = bundle.getString(Key.KEY_ZHANJI_JINZUAN);
		//银钻
		silverdiamond = bundle.getString(Key.KEY_ZHANJI_YINZUAN);
		//金星
		goldstar = bundle.getString(Key.KEY_ZHANJI_JINXING);
		//银星
		silverstar = bundle.getString(Key.KEY_ZHANJI_YINXING);
		//金杯
		goldcup = bundle.getString(Key.KEY_ZHANJI_JINBEI);
		
		multiple = 1;

		tvTerm = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_term);
		tvTime = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_time);
		tvProId = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_programId);
		tvRestCount = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_restCount);
		tvRengouCount = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_rengouCount);
		tvJoinMoney = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_joinMoney);
		tvBalance = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_balance);

		imageFaqiren = (ImageView) findViewById(R.id.shuangseqiuhemaidetailact_image_faqiren);
		tvFaqiren = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_faqiren);
		tvHitCount = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_hitCount);
		tvHitMoney = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_hitMoney);
		tvAllMoney = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_allMoney);
		tvMultiple = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_multiple);
		tvAllCount = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_allCount);
		tvEachMoney = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_eachMoney);
		tvBaodiMoney = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_baodiMoney);
		tvTicheng = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_ticheng);
		tvState = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_state);
		tvJindu = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_jindu);
		tvRestCountInside = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_restCountInside);
		tvIsShow = (TextView) findViewById(R.id.shuangseqiuhemaidetailact_tv_isShow);

		linearContent = (LinearLayout) findViewById(R.id.shuangseqiuhemaidetailact_linear_content);
		progress = ViewerUtil.showProgressDialog(ShuangseqiuHemaiDetailAct.this, "加载中...");
		progress.show();

		btnCancel = (ImageButton) findViewById(R.id.shuangseqiuhemaidetailact_btn_cancel);
		btnCancel.setOnClickListener(this);
		btnConect = (Button) findViewById(R.id.shuangseqiuhemaidetailact_btn_detail);
		btnConect.setOnClickListener(this);
		btnHelp = (ImageButton) findViewById(R.id.shuangseqiuhemaidetailact_btn_help);
		btnHelp.setOnClickListener(this);
		btnSubmit = (Button) findViewById(R.id.shuangseqiuhemaidetailact_btn_submit);
		btnSubmit.setOnClickListener(this);

		progressJoin = ViewerUtil.showProgressDialog(ShuangseqiuHemaiDetailAct.this, Language.POPLOTTERYTIP_BUYING);

		edtJoinCount = (EditText) findViewById(R.id.shuangseqiuhemaidetailact_edt_joinCount);
		linearZhanji = (LinearLayout) findViewById(R.id.shuangseqiuhemaidetailact_linear_zhanji);
		linearLayOutZhanji = (LinearLayout) findViewById(R.id.shuangseqiuhemaidetailact_ll_zhanji);
		loginTimeControl = new LoginTimeControl();

		edtJoinCount.setText(Integer.toString(multiple));
		tvProId.setText(showId);
		tvFaqiren.setText(strfaqiren);
		getZhanji();
		StringBuffer sbTitle = new StringBuffer();
		sbTitle.append(Language.POPLOTTERYTIP_DI).append(termNum).append(Language.POPLOTTERYTIP_QI).append(Language.POPLOTTERYTIP_HEMAI);
		tvTerm.setText(sbTitle.toString());

		tvTime.setText(NO_DATA);
		tvRestCount.setText(NO_DATA);
		tvRengouCount.setText(NO_DATA);

		tvHitCount.setText(NO_DATA);
		tvHitMoney.setText(NO_DATA);
		tvAllMoney.setText(NO_DATA);
		tvMultiple.setText(NO_DATA);
		tvAllCount.setText(NO_DATA);
		tvEachMoney.setText(NO_DATA);
		tvBaodiMoney.setText(NO_DATA);
		tvTicheng.setText(NO_DATA);
		tvState.setText(NO_DATA);
		tvJindu.setText(NO_DATA);
		tvRestCountInside.setText(NO_DATA);

		/** 2011-08-08 city add for balance set */
		tvJoinMoney.setText(NO_DATA);
		tvBalance.setText(NO_DATA);

		hemaiGetDetailCommunicater = new HemaiGetDetailCommunicater();
		getUserIconCommunicater = new GetUserIconCommunicater();
		hemaiJoinCommunicater = new HemaiJoinCommunicater();
		threadGetDetail();
		threadGetUserIcon();

	}


	/**
	 * 战绩显示
	 *kael gaojian@500wan.com
	 */
	private void getZhanji(){
		ShowZhanji zhanji = new ShowZhanji(ShuangseqiuHemaiDetailAct.this);
		// 获取显示战绩星级图片的list
		ArrayList <ImageView> zhanjilist = new ArrayList<ImageView>();
		zhanjilist = zhanji.showzhanji(golddiamond, silverdiamond, goldstar, silverstar, goldcup);
		//判断是否有战绩
		int size = zhanjilist.size();
		if(size == 0){// 没有战绩就不显示战绩的linear
			linearLayOutZhanji.setVisibility(View.GONE);
		}else{// 有战绩就将list中的ImageView添加到显示战绩的linear
			for(int i = 0 ; i < size; i++){
				linearLayOutZhanji.setVisibility(View.VISIBLE);
				linearZhanji.setVisibility(View.VISIBLE);
				linearZhanji.addView(zhanjilist.get(i));
			}
		}
	}

	private void getAvatar() {
		new Thread() {

			@Override
			public void run() {
				super.run();

				Bitmap bm = null;
				String name = HemaiDetailInfo.getHemaiDetailInfo().getUserIcon();
				ImageDB.getImageDB().open(ShuangseqiuHemaiDetailAct.this);

				bm = ImageUtils.getLocalImg(name);

				if (bm == null) {
					bm = ImageUtils.downLoadImg(name);
					if (bm != null) {
						Message msg = handlerHemaiDetail.obtainMessage(REPLACE_IMAGE, bm);
						handlerHemaiDetail.sendMessage(msg);
						ImageUtils.saveImg(name, bm);
					}
				} else {
					Message msg = handlerHemaiDetail.obtainMessage(REPLACE_IMAGE, bm);
					handlerHemaiDetail.sendMessage(msg);
				}
				ImageDB.getImageDB().close();
			}
		}.start();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.shuangseqiuhemaidetailact_btn_cancel:
			ShuangseqiuHemaiDetailAct.this.finish();
			break;

		case R.id.shuangseqiuhemaidetailact_btn_detail:

			break;

		case R.id.shuangseqiuhemaidetailact_btn_help:
			Intent intentToHelp = new Intent();
			intentToHelp.setClass(ShuangseqiuHemaiDetailAct.this, HelpAct.class);
			intentToHelp.putExtra("url", Config.WHATIS_HEMAI_WEB_ADD).putExtra("title", "合买介绍").putExtra("havetitle", Config.NUM1);
			startActivity(intentToHelp);

			break;

		case R.id.shuangseqiuhemaidetailact_btn_submit:
			buyNum = Integer.toString(multiple);
			EDebug.o(ShuangseqiuHemaiDetailAct.this, "buyNum is " + buyNum);
			EDebug.o(ShuangseqiuHemaiDetailAct.this, "multiple is " + multiple);
			if (canJoin) {
				if (UserInfo.getUserInfo().getUserSession().length() < 3) {
					Intent intent = new Intent();
					intent.setClass(ShuangseqiuHemaiDetailAct.this, LoginAct.class);
					startActivity(intent);
				} else {
					if (loginTimeControl.isNeedInputPwd()) {
						loginTimeControl.showInputPasswordDialog(this);
					} else {
						progressJoin.show();
						threadJoin();
					}
				}
			} else {
				ViewerUtil.showTipDialog(ShuangseqiuHemaiDetailAct.this, "请正确填写信息", false);
			}
			break;

		default:
			break;
		}

	}

	private ArrayList<HashMap<String, Object>> getListContent(String strContent) {
		String[] contentItems = strContent.split("\\$");
		ArrayList<HashMap<String, Object>> listContent = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < contentItems.length; i++) {
			HashMap<String, Object> mapItem = new HashMap<String, Object>();
			if (contentItems[i].indexOf(Config.VERTRAL_LINE) > 0) {
				mapItem.put("red", (contentItems[i].substring(0, contentItems[i].indexOf(Config.VERTRAL_LINE))).replace(Config.COMMA, Config.ONESPACE));
				mapItem.put("blue", contentItems[i].substring((contentItems[i].indexOf(Config.VERTRAL_LINE) + 1), contentItems[i].length()).replace(Config.COMMA, Config.ONESPACE));
				EDebug.o(this, "contentItems = " + contentItems[i]);
				EDebug.o(this, "contentItemsred = " + (contentItems[i].substring(0, contentItems[i].indexOf(Config.VERTRAL_LINE))).replace(Config.COMMA, Config.ONESPACE));
				EDebug.o(
						this,
						"contentItemsblue = "
								+ contentItems[i].substring((contentItems[i].indexOf(Config.VERTRAL_LINE) + 1), contentItems[i].length()).replace(Config.COMMA, Config.ONESPACE));
			} else {
				mapItem.put("red", Config.NULLMARK);
				mapItem.put("blue", contentItems[i].replace(Config.COMMA, Config.ONESPACE));
			}
			listContent.add(mapItem);
		}
		return listContent;
	}

	private String strLengthOption(String str, int max) {
		if (str.length() < max + 1) {
			return str;
		} else {
			StringBuffer sb = new StringBuffer();
			sb.append(str.substring(0, max - 2)).append(Config.ELLIPSIS);
			String result = sb.toString();
			return result;
		}
	}

	private Dialog dialogBuySucceed;

	private void buySucceed() {
		dialogBuySucceed = new Dialog(this, R.style.dialog);
		dialogBuySucceed.setContentView(R.layout.viewerutil_dialog_showbuysucceed);
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_msg)).setText("参与合买成功");
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_term)).setVisibility(View.GONE);
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_count)).setText("认购:" + multiple + "份");
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_cost)).setText("消费:" + Language.POPLOTTERYTIP_RMB
				+ getDoubleString(Double.parseDouble(eachMoney) * multiple));
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_balance)).setText("剩余金额:" + Language.POPLOTTERYTIP_RMB
				+ UserInfo.getUserInfo().getUserBalance());
		dialogBuySucceed.show();
		OnClickListener dialogSucceedListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.viewerutildialogshowbuysucceed_btn_back:
					dialogBuySucceed.dismiss();
					ShuangseqiuHemaiDetailAct.this.finish();
					break;

				case R.id.viewerutildialogshowbuysucceed_btn_toHall:
					dialogBuySucceed.dismiss();
					ShuangseqiuHemaiDetailAct.this.finish();
					UserInfo.getUserInfo().setRebuy(true);
					break;

				default:
					break;
				}

			}
		};
		((Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_back)).setOnClickListener(dialogSucceedListener);
		((Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_toHall)).setOnClickListener(dialogSucceedListener);
		dialogBuySucceed.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				ShuangseqiuHemaiDetailAct.this.finish();
			}
		});

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		OptionAct.isLoginSucceed = false;
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

	private String getDoubleString(double _eachMoney) {
		DecimalFormat format = new DecimalFormat("0.00");
		format.setMaximumFractionDigits(3);
		String result = format.format(_eachMoney);
		return result;
	}

	private String getLotRestBottom() {

		double userRestTemp;

		userRestTemp = Double.parseDouble(UserInfo.getUserInfo().getUserBalance());

		double doubelTemp = userRestTemp - Double.parseDouble(eachMoney) * multiple;
		EDebug.o(LotteryUtils.class, "input userRestTemp - buying is : " + String.valueOf(doubelTemp));
		DecimalFormat format = new DecimalFormat("0.00");
		String afterBet = Language.POPLOTTERYTIP_RMB + format.format(doubelTemp);
		return afterBet;
	}

}
